<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主键" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老师姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入老师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学科" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入学科"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="校区" prop="campus">
        <el-input
          v-model="queryParams.campus"
          placeholder="请输入校区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:teacher:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:teacher:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:teacher:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:teacher:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="importExcel"
        >导入
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="老师姓名" align="center" prop="name"/>
      <el-table-column label="学科" align="center" prop="subject"/>
      <el-table-column label="校区" align="center" prop="campus"/>
      <el-table-column label="电话" align="center" prop="phone"/>
      <el-table-column label="帮扶优先级" align="center" prop="priority"/>
      <el-table-column label="类型" align="center" prop="teacherTypeObj.typeName"/>
      <el-table-column label="性别" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender == 0">男</el-tag>
          <el-tag v-if="scope.row.gender == 1" style="background-color: pink;color: black">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:teacher:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:teacher:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老师中心对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="校区">
          <el-select v-model="form.campusId"
                     @change="campusHandleChange"
                     placeholder="校区">
            <el-option
              v-for="item in campusList"
              :key="item.id"
              :label="item.campusName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学科">
          <el-select v-model="form.subjectId" placeholder="学科">
            <el-option
              v-for="item in subjectsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老师类型">
          <el-select v-model="form.teacherType" placeholder="老师类型">
            <el-option
              v-for="item in teacherTypeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.teacherType"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入帮扶优先级"/>
        </el-form-item>
        <el-form-item label="上级组长">
          <el-select v-model="form.superiors" placeholder="上级组长">
            <el-option
              v-for="item in superiorsObjs"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="绩效类型">
          <el-select v-model="form.superiors" placeholder="绩效类型">
            <el-option
              v-for="item in superiorsObjs"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--上传界面-->
    <el-dialog title="老师上传" :visible.sync="importFormVisible" :close-on-click-modal="false">
      <el-form :model="importForm" label-width="80px" ref="importForm">
        <el-form-item>
            <el-button @click="handleExportTemplate" type="primary">下载模板</el-button>
        </el-form-item>
        <el-form-item>
          <el-upload
            class="upload-demo"
            :action="url"
            multiple
            :limit="1"
            :headers="headers"
            :on-success="ExcelImportSuccess"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import {
  addTeacher,
  delTeacher,
  listCampus,
  listSubjects,
  listTeacher,
  listTeacherType,
  loadSuperiorsObjs,
  updateTeacher
} from "@/api/teacher/teacher";
import {getToken} from "@/utils/auth";

export default {
  name: "Teacher",
  data() {
    return {
      url: process.env.VUE_APP_BASE_API + "/teacher/teacher/import",
      headers: {Authorization: "Bearer " + getToken()},
      fileList: [],
      importForm: {},
      importFormVisible: false,
      superiorsObjs: [],
      teacherTypeList: [],
      campusList: [],
      subjectsList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师中心表格数据
      teacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        subject: null,
        campus: null,
        phone: null,
        priority: null,
        teacherType: null,
        gender: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getToken,
    ExcelImportSuccess(file) {
      if (file.code==200) {
        this.$message.success('导入成功');
        this.importFormVisible = false;
        this.getList();
        this.fileList = [];
      } else {
        this.$message.error(file.message);
      }
    },


    //文件导入
    importExcel() {
      this.importFormVisible = true;
    },


    //选中校区获取学科
    campusHandleChange(res) {
      for (let i = 0; i < this.campusList.length; i++) {
        if (this.campusList[i].id == res) {
          this.subjectsList = this.campusList[i].subjects;
          this.form.campus = this.campusList[i].campusName;
          console.log(this.subjectsList)
          return;
        }
      }
    },
    //查询校区
    getListCampus() {
      loadSuperiorsObjs().then(res => {
        let {code, data} = res;
        if (code == 200) {
          this.superiorsObjs = data;
        }
      })
      listTeacherType().then(res => {
        let {code, data} = res;
        if (code == 200) {
          this.teacherTypeList = data;
        }
      })
      listCampus().then(res => {
        let {code, data} = res;
        if (code == 200) {
          this.campusList = data;
        }
      })
    },
    //查询学科
    getListSubjects() {
      listSubjects().then(res => {
        let {code, data} = res;
        if (code == 200) {
          this.subjectsList = data;
        }
      })
    },
    /** 查询老师中心列表 */
    getList() {
      this.loading = true;
      listTeacher(this.queryParams).then(response => {
        this.teacherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        subject: null,
        campus: null,
        phone: null,
        priority: null,
        teacherType: null,
        gender: null,
        userId: null,
        deleted: null,
        campusId: null,
        subjectId: null,
        superiors: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加老师中心";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getListCampus();
      this.getListSubjects();
      this.reset();
      let assign = JSON.parse(JSON.stringify(row));
      this.form = assign;
      this.form.campusId = Number(assign.campusId);
      this.form.subjectId = Number(assign.subjectId);
      this.open = true;
      this.title = "修改老师中心";

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeacher(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeacher(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除老师中心编号为"' + ids + '"的数据项？').then(function () {
        return delTeacher(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/teacher/export', {
        ...this.queryParams
      }, `teacher_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleExportTemplate() {
      this.download('teacher/teacher/exportTemplate', {
        ...this.queryParams
      }, `teacher_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
